/**	CODE EVAL::Moderate [Array Absurdity]
 *	Imagine we have an immutable array of size N which we know to be filled with 
 *	integers ranging from 0 to N-2, inclusive. Suppose we know that the array 
 *	contains exactly one duplicated entry and that duplicate appears exactly twice. 
 *	Find the duplicated entry. (For bonus points, ensure your solution has constant 
 *	space and time proportional to N)
 *
 *	Solution by Michael Pio Arroyo
 */

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>

int main(int argc, char** argv) {
	std::ifstream file;
	std::string line;
	file.open(argv[1]);
	
	while(getline(file, line)) {
		std::string::iterator it = line.begin();
		std::string t;
		std::string s;
		// vector array size
		while(*it != ';') {
			s = *it;
			t.append(s.c_str());
			++it;
		}
		int count = atoi(t.c_str());
		t.clear();
		++it;
		// create vector array of ints
		std::vector<int> list;
		int dist = std::distance(it, line.end());
		while(it != line.end()) {
			for(int i=0; i<dist; ++i) {
				if(*it != ',') {
					s = *it;
					t.append(s.c_str());
					++it;
				} else {
					list.push_back(atoi(t.c_str()));
					t.clear();
					++it;
				}
			}
			list.push_back(atoi(t.c_str()));
			t.clear();
		}
		// now check for doubles
		bool found = false;
		int j = 0;
		int x = list.at(j);
		while(!found) {
			x = list.at(j);
			for(int i=j+1; i<count; ++i) {
				if(list.at(i) == x) {
					std::cout << x << '\n';
					found = true;
				}
			}
			++j;
		}
		list.clear();
	}

	file.close();
	return 0;
}